package LeetCode.month02.day55;

import java.text.CollationElementIterator;
import java.util.*;

public class No2349 {
}
class NumberContainers {
    Map<Integer, TreeSet<Integer>> minindex = new HashMap<>();
    Map<Integer,Integer> nums = new HashMap<>();
    public NumberContainers() {

    }

    public void change(int index, int number) {
        if(nums.containsKey(index)){
            int pre = nums.get(index);
            TreeSet<Integer> oldset = minindex.get(pre);
            oldset.remove(index);
            if(oldset.size()==0) minindex.remove(pre);
            else minindex.put(pre,oldset);
            TreeSet<Integer> set = minindex.get(number);
            if(set==null) set = new TreeSet<>();
            set.add(index);
            minindex.put(number,set);
        }
        nums.put(index,number);
        TreeSet<Integer> set = minindex.get(number);
        if(set==null) set = new TreeSet<>();
        set.add(index);
        minindex.put(number,set);
    }

    public int find(int number) {
        if(!minindex.containsKey(number)){
            return -1;
        }
        return minindex.get(number).first();
    }
}
